package com.web.manager.service;



import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.web.manager.exception.ServiceException;
import com.web.manager.mapper.UserMapper;
import com.web.manager.pojo.User;
import com.web.manager.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserService extends ServiceImpl<UserMapper,User> {

    @Resource
    private UserMapper userMapper;


    /*
    public void insertUser(User user) {

        userMapper.Insert(user);
    }

    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);
    }

    public void batchDeleteUser(List<Integer> ids) {
        for (Integer id:ids){
            userMapper.deleteUser(id);
        }

    }

    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }



    public Page<User> selectByPage(Integer pageNum, Integer pageSize, String username, String name) {
        Integer skipNum = (pageNum - 1) * pageSize;  // 计算出来  1 -> 0,5    2 -> 5,5   3 -> 10,5

        Page<User> page = new Page<>();
        List<User> userList = userMapper.selectByPage(skipNum, pageSize, username, name);
        Integer total = userMapper.selectCountByPage(username, name);
        page.setTotal(total);
        page.setList(userList);
        return page;
    }


     */

    @Override
    public boolean save(User entity){
        if(StrUtil.isBlank(entity.getName())){
            entity.setName(entity.getUsername());  //默认姓名与账号一致
        }
        if(StrUtil.isBlank(entity.getPassword())){
            entity.setPassword("123");   //默认密码123
        }
        if(StrUtil.isBlank(entity.getRole())){
            entity.setRole("学生");   //默认角色
        }

        return super.save(entity);
    }


    public User selectByUsername(String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //条件查询器
        queryWrapper.eq("username",username);  // ==> where username = #{username}


        // 根据用户名查询数据库的用户信息
        return  this.getOne(queryWrapper); // ==>select * from manager_user where username = #{username}
    }


    // 验证用户账户是否合法
    public User login(User user) {
       /* QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //条件查询器
        queryWrapper.eq("username",user.getUsername());  // ==> where username = #{username}


        // 根据用户名查询数据库的用户信息
        User dbUser = userMapper.selectOne(queryWrapper); // ==>select * from manager_user where username = #{username}
        */

        User dbUser = selectByUsername(user.getUsername());
        if (dbUser == null) {
            // 抛出一个自定义的异常
            throw new ServiceException("用户名或密码错误");
        }
        if (!user.getPassword().equals(dbUser.getPassword())) {
            throw new ServiceException("用户名或密码错误");
        }
        //验证成功后，生成token
        String token = TokenUtils.createToken(dbUser.getId().toString(),dbUser.getPassword()); //不是user.getId(),因为没有输入id
        dbUser.setToken(token);
        return dbUser;
    }

    public User register(User user) {
        User dbUser = selectByUsername(user.getUsername());
        if (dbUser != null) {
            // 抛出一个自定义的异常
            throw new ServiceException("用户名已存在");
        }

        userMapper.insert(user);
        return user;
    }

    public void resetPassword(User user) {
        User dbUser = selectByUsername(user.getUsername());
        if(dbUser == null){
            throw new ServiceException("用户名不存在");
        }
        if( !dbUser.getPhone().equals(dbUser.getPhone())){
            throw new ServiceException("验证错误");
        }
        dbUser.setPassword("123");      //重置密码
        userMapper.updateById(dbUser);
    }
}
